package il.ac.braude.sona.control;
import il.ac.braude.sona.constants.ButtonPermission;
import il.ac.braude.sona.constants.Mssg;
import il.ac.braude.sona.constants.P;
import il.ac.braude.sona.constants.TestingParamKit;
import il.ac.braude.sona.entities.ChangeRequest;
import il.ac.braude.sona.gui.infogui.RequsetInfoFrame;
import il.ac.braude.sona.ocsf.client.Client;
import il.ac.braude.sona.ocsf.client.ServerResponseListener;

import javax.swing.JOptionPane;

/**
 * The FreezingReqController contains all methods to handle freeze and thawing request
 *  
 * @author Ofek Ron
 * @author Avihay Avitan
 * @author Nitzan Inbar
 * @author Sasha Rozenthal
 */
public class FreezingReqController{

	/**
	 * When supervisor press freeze request, this method sends message to server 
	 * to perform freeze request and notify user with answer
	 * 
	 * @param req - the change request involved
	 * @param requsetInfoFrame - the request info fram
	 */
	public void execFreezingRequest(ChangeRequest req,final RequsetInfoFrame requsetInfoFrame) {
		try {
			Client c = Client.getInstance();
			Mssg mssg= new Mssg(ButtonPermission.FreezeRequest,req.getID());
			c.sendToServer(new ServerResponseListener() {
				@Override
				public void onServerResponse(Mssg mssg) {
					TestingParamKit.getTestingKit().notifyTester();
					if (TestingParamKit.getTestingKit().TestMode) {
						if (mssg.is(P.error)) { JOptionPane.showMessageDialog(requsetInfoFrame,"You are not the supervisor!"); }
						else {
							JOptionPane.showMessageDialog(requsetInfoFrame,"All request processes are SUSPENDED!");
							requsetInfoFrame.dispose();
						}
						
					}
					if (mssg.is(P.error)) {
						JOptionPane.showMessageDialog(requsetInfoFrame,"cant update close to DB");
						return;
					}
					
					else {
						JOptionPane.showMessageDialog(requsetInfoFrame,"All request processes are SUSPENDED!");
						requsetInfoFrame.dispose();
						requsetInfoFrame.dispose();
					}
					
					}
			},mssg);
		 
		} catch (Exception e) {
			JOptionPane.showMessageDialog(requsetInfoFrame,"cant connect to DB");
		}
		
	}
	/**
	 * When manager press thaw request, this method sends message to server 
	 * to perform thawing request and notify user with answer
	 * 
	 * @param req - the change request involved
	 * @param requsetInfoFrame - the request info fram
	 */
	public void execThawingRequest(ChangeRequest req,final RequsetInfoFrame requsetInfoFrame) {
		try {
			Client c = Client.getInstance();
			Mssg mssg= new Mssg(ButtonPermission.UnFreezeRequest,req.getID());
			c.sendToServer(new ServerResponseListener() {
				@Override
				public void onServerResponse(Mssg mssg) {
					if (mssg.is(P.error)) {
						JOptionPane.showMessageDialog(requsetInfoFrame,"cant update close to DB");
						return;
					}
					
					else {
						JOptionPane.showMessageDialog(requsetInfoFrame,"Request unfreezed!");
						requsetInfoFrame.dispose();
						requsetInfoFrame.dispose();
					}
					
					}
			},mssg);
		 
		} catch (Exception e) {
			JOptionPane.showMessageDialog(requsetInfoFrame,"cant connect to DB");
		}
		
	}






}
